package com.yanq.liancar.activity;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.gyf.barlibrary.ImmersionBar;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;
import com.vondear.rxtool.RxActivityTool;
import com.vondear.rxtool.view.RxToast;
import com.yanq.liancar.R;
import com.yanq.liancar.activity.base.BaseToolbarActivity;
import com.yanq.liancar.adapter.GoodsDetailesImgAdapter;
import com.yanq.liancar.bean.GoodsDetailesBean;
import com.yanq.liancar.bean.MyBanner;
import com.yanq.liancar.url.Config;
import com.yanq.liancar.util.GlideImageLoader;
import com.yanq.liancar.util.Utils;
import com.youth.banner.Banner;
import com.youth.banner.BannerConfig;
import com.youth.banner.listener.OnBannerListener;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class SmallBGoodsDetailsActivity extends BaseToolbarActivity {
    @BindView(R.id.banner)
    Banner banner;
    @BindView(R.id.img_back)
    ImageView imgBack;
    @BindView(R.id.tv_goodsname)
    TextView tvGoodsname;
    @BindView(R.id.tv_goodsprice)
    TextView tvGoodsprice;
    @BindView(R.id.tv_soldnum)
    TextView tvSoldnum;
    @BindView(R.id.tv_jianjie)
    TextView tvJianjie;
    @BindView(R.id.rl_content)
    RelativeLayout rlContent;
    @BindView(R.id.tv_kefu)
    TextView tvKefu;
    @BindView(R.id.tv_buynow)
    TextView tvBuynow;
    @BindView(R.id.img_recyclerview)
    RecyclerView mRecyclerView;
    private List<MyBanner> mBanners;
    private int goodsID;
    private List<GoodsDetailesBean.DataBean.B2ShopGoodsPhotoDetailsBean> mB2ShopGoodsPhotoDetailsBeans=new ArrayList<>();
    private List<GoodsDetailesBean.DataBean.B2ShopGoodsPhotoSlideShowBean> mB2ShopGoodsPhotoSlideShowBeans=new ArrayList<>();
    private GoodsDetailesBean.DataBean mDataBean;
    private List<String> imageUrl;
    private GoodsDetailesImgAdapter adapter;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//        ImmersionBar.with(this).transparentStatusBar().init();
        setContentView(R.layout.activity_smallbgoodsdetails);
        goodsID=getIntent().getExtras().getInt("goodsID");
        ButterKnife.bind(this);
        initView();
        initOkgo();
    }

    private void initBannerData() {
        banner.setImageLoader(new GlideImageLoader())
                .setImages(imageUrl)//设置图片集合
                .setDelayTime(3000)//设置轮播时间
                .setBannerStyle(BannerConfig.CIRCLE_INDICATOR)//设置指示器位置（当banner模式中有指示器时）
                .setIndicatorGravity(BannerConfig.CENTER)
                .start();//banner设置方法全部调用完毕时最后调用
        banner.setOnBannerListener(new OnBannerListener() {
            @Override
            public void OnBannerClick(int position) {
                //ToastUtils.showToast(SmallBGoodsDetailsActivity.this, "您点击了第" + (mBanners.get(position).type + 1) + "张照片");
            }
        });
    }

    @OnClick({R.id.img_back, R.id.tv_kefu, R.id.tv_buynow})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.img_back:
                RxActivityTool.finishActivity(this);
                break;
            case R.id.tv_kefu:
                break;
            case R.id.tv_buynow:
                if (mDataBean!=null){
                    Bundle bundle=new Bundle();
                    bundle.putSerializable("DataBean",mDataBean);
                    RxActivityTool.skipActivity(this,SmallBShopOrderActivity.class,bundle);
                }
                break;
        }
    }


    protected void initView() {
        mRecyclerView.setNestedScrollingEnabled(false);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        adapter = new GoodsDetailesImgAdapter(R.layout.item_goodsdetailes, mB2ShopGoodsPhotoDetailsBeans);
        mRecyclerView.setAdapter(adapter);
    }
    private void initOkgo() {
        OkGo.<String>post(Config.s + "llc/api/selectProductDetails")
                .tag(this)
                .params("goodsid",goodsID)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        Gson gson = new Gson();
                        GoodsDetailesBean selectClassifyBean = gson.fromJson(response.body(), GoodsDetailesBean.class);
                        if (selectClassifyBean.isIsSuccess()) {
                            mDataBean=selectClassifyBean.getData();
                            if (mDataBean!=null){
                                mB2ShopGoodsPhotoSlideShowBeans=mDataBean.getB2ShopGoodsPhotoSlideShow();
                                mB2ShopGoodsPhotoDetailsBeans=mDataBean.getB2ShopGoodsPhotoDetails();
                                adapter.setNewData(mB2ShopGoodsPhotoDetailsBeans);
                                imageUrl=new ArrayList<>();
                                for (int i = 0; i <mB2ShopGoodsPhotoSlideShowBeans.size() ; i++) {
                                    imageUrl.add(mB2ShopGoodsPhotoSlideShowBeans.get(i).getWebPath());
                                }
                                initBannerData();
                                tvGoodsname.setText(mDataBean.getGoods_name());
                                tvGoodsprice.setText("¥"+mDataBean.getSale_price());
                                tvSoldnum.setText("已售"+mDataBean.getSale_num()+"件");
                                tvJianjie.setText(mDataBean.getGoods_introduction());
                            }
                        }else {
                            RxToast.error(selectClassifyBean.getOutputMessage());
                        }
                    }

                    @Override
                    public void onError(Response<String> response) {
                        super.onError(response);
                        RxToast.error("服务器连接错误");
                    }
                });
    }
}
